package com.chj.comp.common.rabbitmq.util;

import com.alibaba.fastjson.JSON;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageDeliveryMode;

import java.util.UUID;

/**
 * @author ：cuihui
 * @date ：Created in 2022/6/11
 * @description：message工具类
 * @modified By：
 */
public class MessageUtil {
    public static Message createMessage(Object o){
        String msgId = UUID.randomUUID().toString();
        // 消息体
        Message message = MessageBuilder.withBody(JSON.toJSONBytes(o)).build();
        // 消息头(持久化、过期时间、编码、消息id)
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        message.getMessageProperties().setExpiration("100");
        message.getMessageProperties().setContentEncoding("UTF-8");
        message.getMessageProperties().setMessageId(msgId);

        return message;
    }
}
